package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MotoInfoMapper;
import com.ruoyi.system.domain.MotoInfo;
import com.ruoyi.system.service.IMotoInfoService;
import org.springframework.util.CollectionUtils;

/**
 * 摩托基础信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-12
 */
@Service
public class MotoInfoServiceImpl implements IMotoInfoService 
{

    private static final Logger log = LoggerFactory.getLogger(MotoInfoServiceImpl.class);

    @Autowired
    private MotoInfoMapper motoInfoMapper;

    /**
     * 查询摩托基础信息
     * 
     * @param infoId 摩托基础信息主键
     * @return 摩托基础信息
     */
    @Override
    public MotoInfo selectMotoInfoByInfoId(Long infoId)
    {
        return motoInfoMapper.selectMotoInfoByInfoId(infoId);
    }

    /**
     * 查询摩托基础信息列表
     * 
     * @param motoInfo 摩托基础信息
     * @return 摩托基础信息
     */
    @Override
    public List<MotoInfo> selectMotoInfoList(MotoInfo motoInfo)
    {
        return motoInfoMapper.selectMotoInfoList(motoInfo);
    }

    /**
     * 新增摩托基础信息
     * 
     * @param motoInfo 摩托基础信息
     * @return 结果
     */
    @Override
    public int insertMotoInfo(MotoInfo motoInfo)
    {
        motoInfo.setCreateTime(DateUtils.getNowDate());
        return motoInfoMapper.insertMotoInfo(motoInfo);
    }

    /**
     * 修改摩托基础信息
     * 
     * @param motoInfo 摩托基础信息
     * @return 结果
     */
    @Override
    public int updateMotoInfo(MotoInfo motoInfo)
    {
        motoInfo.setUpdateTime(DateUtils.getNowDate());
        return motoInfoMapper.updateMotoInfo(motoInfo);
    }

    /**
     * 批量删除摩托基础信息
     * 
     * @param infoIds 需要删除的摩托基础信息主键
     * @return 结果
     */
    @Override
    public int deleteMotoInfoByInfoIds(Long[] infoIds)
    {
        return motoInfoMapper.deleteMotoInfoByInfoIds(infoIds);
    }

    /**
     * 删除摩托基础信息信息
     * 
     * @param infoId 摩托基础信息主键
     * @return 结果
     */
    @Override
    public int deleteMotoInfoByInfoId(Long infoId)
    {
        return motoInfoMapper.deleteMotoInfoByInfoId(infoId);
    }

    @Override
    public String importUser(List<MotoInfo> motoList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(motoList) || motoList.isEmpty())
        {
            throw new ServiceException("导入摩托车数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MotoInfo moto : motoList)
        {
            try
            {
                // 验证是否存在这个用户
                String carOwnerIdCard = moto.getCarOwnerIdCard();
                if (carOwnerIdCard == null) {
                    continue;
                }
                List<MotoInfo> foundMotoLists = motoInfoMapper.selectMotoInfoByCarOwnerIdCard(carOwnerIdCard);
                if (CollectionUtils.isEmpty(foundMotoLists)) {
                    moto.setCreateBy(operName);
                    motoInfoMapper.insertMotoInfo(moto);
                    successNum++;
                } else {
                    List<String> carNumberLists = foundMotoLists.stream().map(MotoInfo::getCarNumber).collect(Collectors.toList());
                    if (!carNumberLists.contains(moto.getCarNumber())) {
                        moto.setCreateBy(operName);
                        motoInfoMapper.insertMotoInfo(moto);
                        successNum++;
                    }
                    else
                    {
                        failureNum++;
                    }
                }

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、车牌号 " + moto.getCarNumber() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        successMsg.insert(0, "数据导入成功！共 " + successNum + " 条");

        return successMsg.toString();
    }
}
